package com.guodefu.demo.config;

import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.containers.KafkaContainer;
import org.testcontainers.containers.PostgreSQLContainer;

@Configuration
public class CommandLineAppStartupRunner {

    @Bean
    public CommandLineRunner commandLineRunner(
            PostgreSQLContainer<?> postgresqlDBContainer,
            GenericContainer<?> redisContainer,
            MongoDBContainer mongoDBContainer,
            KafkaContainer kafkaContainer) {
        return args -> {
            // 打印 postgresql 信息
            String host = postgresqlDBContainer.getHost();
            Integer port = postgresqlDBContainer.getMappedPort(5432);
            String username = postgresqlDBContainer.getUsername();
            String password = postgresqlDBContainer.getPassword();
            String database = postgresqlDBContainer.getDatabaseName();

            System.out.println("PostgreSQL Container started at:");
            System.out.println("Host: " + host);
            System.out.println("Port: " + port);
            System.out.println("Database: " + database);
            System.out.println("Username: " + username);
            System.out.println("Password: " + password);

            // 打印 Redis 信息
            String redisHost = redisContainer.getHost();
            Integer redisPort = redisContainer.getMappedPort(6379);
            System.out.println("Redis Container started at:");
            System.out.println("Host: " + redisHost);
            System.out.println("Port: " + redisPort);

            // 打印 MongoDB 信息
            String mongoHost = mongoDBContainer.getHost();
            Integer mongoPort = mongoDBContainer.getMappedPort(27017);
            System.out.println("MongoDB Container started at:");
            System.out.println("Host: " + mongoHost);
            System.out.println("Port: " + mongoPort);

            // 打印 Kafka 信息
            String kafkaBootstrapServers = kafkaContainer.getBootstrapServers();
            System.out.println("Kafka Container started at:");
            System.out.println("Bootstrap Servers: " + kafkaBootstrapServers);
        };
    }
}
